package com.ws.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ws.commonutils.R;
import com.ws.eduservice.entity.EduCourse;
import com.ws.eduservice.entity.vo.CourseInfoForm;
import com.ws.eduservice.entity.vo.CoursePublishVo;
import com.ws.eduservice.entity.vo.CourseQuery;
import com.ws.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("list/{current}/{limit}")
    public R list(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(current, limit);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQuery.getTitle())){
            queryWrapper.like("title", courseQuery.getTitle());
        }
        if (!StringUtils.isEmpty(courseQuery.getTeacherId())){
            queryWrapper.eq("teacher_id", courseQuery.getTeacherId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())){
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getStatus())){
            queryWrapper.eq("status", courseQuery.getStatus());
        }
        eduCourseService.page(page, queryWrapper);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();
        return R.ok().data("list", list).data("total", total);


    }
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId=eduCourseService.addCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId);
    }

    @GetMapping("getCourseInfo/{couserId}")
    public R getCourseInfo(@PathVariable String couserId){
       CourseInfoForm courseInfoForm= eduCourseService.getCourseInfo(couserId);
       return R.ok().data("courseInfo", courseInfoForm);
    }

    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String courseId=eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok().data("courseId", courseId);

    }
    @GetMapping("getCoursePublishVo/{couserId}")
    public R getCoursePublishVoInfo(@PathVariable String couserId){
        CoursePublishVo coursePublishVo = eduCourseService.getCoursePublishVoById(couserId);
        return R.ok().data("coursePublishVo", coursePublishVo);
    }

    //课程最终发布
    @PostMapping("finalPublish/{id}")
    public R finalPublish(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean b = eduCourseService.updateById(eduCourse);
        if(!b){
            return R.error().message("发布失败");
        }
        return R.ok();
    }

    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        Boolean res=eduCourseService.deleteCourseById(courseId);
        if(!res){
            return R.error().message("删除课程失败");
        }
        return R.ok();
    }
}

